/**
 * [fcNotice]
 * @基于jQuery
 * @ver: 1.0
 * @date: 2021-7-5
 * @author: cm.yu
 */

let message = {};
(function(pop){
	var options = {
		target: 'body',		//在什么dom内创建dialog
		closeBtn: false, 	//是否有关闭按钮
		duration:3000,		//显示时间, 毫秒。设为 0 则不会自动关闭
		position:'center',	//显示位置默认居中
	}
	var elem = '.message-notice',
		_fadeOutClass = 'notice-slide-fadeOut',
		_fadeInClass = 'notice-slide-fadeIn',
		noticeFlag = true;

	pop.info = function(content,opts){
		opts = $.extend({}, options, opts);
		opts["type"] = 'info';
		create(content,opts);
	};

	pop.success = function(content,opts){
		opts = $.extend({}, options, opts);
		opts["type"] = 'success';
		create(content,opts);
	};

	pop.error = function(content,opts){
		opts = $.extend({}, options, opts);
		opts["type"] = 'error';
		create(content,opts);
	};

	pop.warning = function(content,opts){
		opts = $.extend({}, options, opts);
		opts["type"] = 'warning';
		create(content,opts);
	};

	pop.loading = function(content,opts){
		opts = $.extend({}, options, opts);
		opts["type"] = 'loading';
		create(content,opts);
	};

	var icon = {
		'info':'<svg t="1586399045233" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4625" width="15" height="15"><path d="M522.318769 273.329231a58.919385 58.919385 0 1 0 58.919385-58.604308 57.186462 57.186462 0 0 0-58.919385 58.604308zM391.325538 476.790154a280.339692 280.339692 0 0 0 0 34.422154l51.357539-61.833846a40.014769 40.014769 0 0 1 29.223385-17.565539 13.312 13.312 0 0 1 7.876923 16.462769l-85.07077 281.757539a56.792615 56.792615 0 0 0 53.878154 72.625231 176.443077 176.443077 0 0 0 144.068923-102.4 277.740308 277.740308 0 0 0 0.945231-35.524924l-51.436308 61.912616a41.275077 41.275077 0 0 1-30.168615 17.565538 13.154462 13.154462 0 0 1-7.876923-14.887384l85.622154-283.175385a57.816615 57.816615 0 0 0-53.090462-72.861538 219.293538 219.293538 0 0 0-145.329231 103.502769zM512.393846 0a512 512 0 1 1-512 512 512 512 0 0 1 512-512z" fill="#ffffff" p-id="4626"></path></svg>',
		'success':'<svg t="1586399279307" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5668" width="15" height="15"><path d="M512 1024C229.2352 1024 0 794.7648 0 512S229.2352 0 512 0s512 229.2352 512 512-229.2352 512-512 512z m271.5264-653.7088l-54.2976-54.3104-307.7376 307.7376-126.72-126.72-54.2976 54.3104L385.28 696.128l36.1984 36.1984z" fill="#ffffff" p-id="5669"></path></svg>',
		'error':'<svg t="1586398977558" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3454" width="15" height="15"><path d="M512.345 2.207c-281.254 0-509.254 228-509.254 509.254s228 509.254 509.254 509.254 509.253-228 509.253-509.253-228-509.255-509.253-509.255z m223.847 663.93c19.628 19.672 19.628 51.58 0 71.252-9.813 9.836-22.676 14.76-35.538 14.76-12.861 0-25.724-4.925-35.539-14.76l-153.14-153.644-153.14 153.642c-9.814 9.836-22.677 14.76-35.532 14.76-12.869 0-25.724-4.925-35.538-14.76-19.628-19.672-19.628-51.58 0-71.253l153.14-153.648-153.141-153.641c-19.628-19.672-19.628-51.58 0-71.253 19.628-19.665 51.442-19.665 71.07 0l153.14 153.642 153.141-153.642c19.635-19.665 51.442-19.665 71.078 0 19.628 19.672 19.628 51.58 0 71.253L583.051 512.486l153.141 153.65z" fill="#ffffff" p-id="3455"></path></svg>',
		'warning':'<svg t="1586399013003" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4345" width="16" height="16"><path d="M0 0h1024v1024H0z" fill="#F8F8F8" fill-opacity="0" p-id="4346"></path><path d="M512 51.2a460.8 460.8 0 1 1 0 921.6 460.8 460.8 0 0 1 0-921.6zM438.272 281.0368l9.8304 246.3744c1.3824 34.304 29.5936 61.3888 63.8976 61.3888s62.464-27.136 63.744-61.44l9.216-246.272A73.5232 73.5232 0 0 0 511.488 204.8a73.3184 73.3184 0 0 0-73.216 76.2368zM435.2 741.888v0.512A76.8 76.8 0 0 0 512 819.2a76.288 76.288 0 0 0 76.288-76.8v-0.512A76.8 76.8 0 0 0 511.488 665.6a76.288 76.288 0 0 0-76.288 76.288z" fill="#ffffff" p-id="4347"></path></svg>',
		'close':'<svg t="1618303899071" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2025" width="18" height="18"><path d="M810.666667 273.493333L750.506667 213.333333 512 451.84 273.493333 213.333333 213.333333 273.493333 451.84 512 213.333333 750.506667 273.493333 810.666667 512 572.16 750.506667 810.666667 810.666667 750.506667 572.16 512z" p-id="2026" fill="#ffffff"></path></svg>',
		'loading':'<svg viewBox="25 25 50 50" class="circular"><circle cx="50" cy="50" r="20" fill="none" class="path"></circle></svg>'
	}

	function create(content,opts) {
       	if(!noticeFlag) return false;
    	noticeFlag = false;		
		var close = '<em>'+icon.close+'</em>';
		$(opts.target).append('<div class="message-notice '+_fadeInClass+' '+opts.type+'" '+(opts.offset?"offset="+opts.offset:"")+' '+(opts.target!="body"?"style='position:absolute;'":"")+'><p><i>'+icon[opts.type]+'</i>' + content + (opts.closeBtn?close:"")+'</p></div>');
        if(opts.cancel) {
            cancel(opts.cancel);
        }else{
            listen(opts.duration,opts.onClose);
        }
	}

	pop.close = function(cb) {
		$(elem).addClass(_fadeOutClass);
		$(elem).removeClass(_fadeInClass);
		 setTimeout(function(){
			$(elem).remove();
			noticeFlag = true;
			if(cb) return cb();
		},300)
	}

	function cancel(cb) {
        return cb();
	}

	function listen(duration,cb) {
		if(duration > 0){
			var timer = setTimeout(function(){
				pop.close(cb);
			},duration);
		}
		$(document).off().on('click',''+elem+' p em',function(){
			pop.close(cb);
			clearTimeout(timer)
		})
	}
})(message);
if( typeof module === "object" && typeof module.exports === "object" ){
	module.exports = message
}